/**
 * 作者: hxl
 * 日期: 2025-05-17 17:28:07
 * 备注: 用户联盟乱斗信息表
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserUnionScuffleGift;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserUnionScuffleGiftModel extends BaseModel implements Model {
    public static final String LISTKEY = "act_list_key.gift_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // 玩家id
    protected long userId; // 玩家id
    protected long actId; // 活动id
    protected int giftId; // 礼包id
    protected int buyNum; // 已购买次数
    protected long resetTime; // 重置时间
    protected String actListKey = ""; // 活动分组key

    public void reset() {
    }

    protected String getKey() {
        return "UserUnionScuffleGift." + actListKey + "." + giftId;
    }

    protected String getListKey() {
        return "UserUnionScuffleGift." + actListKey;
    }

    protected String getListItemKey() {
        return giftId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_union_scuffle_gift"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, userId, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, userId, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // 玩家id
        obj.put("userId", userId); // 玩家id
        obj.put("actId", actId); // 活动id
        obj.put("giftId", giftId); // 礼包id
        obj.put("buyNum", buyNum); // 已购买次数
        obj.put("resetTime", resetTime); // 重置时间
        obj.put("actListKey", actListKey); // 活动分组key
        return obj;
    }

    public PBUserUnionScuffleGift.Builder takeInitBuilder() {
        PBUserUnionScuffleGift.Builder sb = PBUserUnionScuffleGift.newBuilder();
        if (giftId != 0) {
            sb.setGiftId(giftId);
        }
        if (buyNum != 0) {
            sb.setBuyNum(buyNum);
        }
        return sb;
    }

    public PBUserUnionScuffleGift.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserUnionScuffleGift.Builder sb = PBUserUnionScuffleGift.newBuilder();
        if (isFieldUpdate("giftId")) {
            sb.setGiftId(giftId);
        }
        if (isFieldUpdate("buyNum")) {
            sb.setBuyNum(buyNum);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public long getUserId() {
        return userId;
    }

    public void putUserId(long userId) {
        if (this.userId != userId) {
            this.userId = userId;
            updateRedisHashField(getKey(), "userId", this.userId);
        }
    }

    public void addUserId(long value) {
        this.userId += value;
        updateRedisHashField(getKey(), "userId", this.userId);
    }

    public long getActId() {
        return actId;
    }

    public void putActId(long actId) {
        if (this.actId != actId) {
            this.actId = actId;
            updateRedisHashField(getKey(), "actId", this.actId);
        }
    }

    public void addActId(long value) {
        this.actId += value;
        updateRedisHashField(getKey(), "actId", this.actId);
    }

    public int getGiftId() {
        return giftId;
    }

    public void putGiftId(int giftId) {
        if (this.giftId != giftId) {
            this.giftId = giftId;
            updateRedisHashField(getKey(), "giftId", this.giftId);
        }
    }

    public void addGiftId(int value) {
        this.giftId += value;
        updateRedisHashField(getKey(), "giftId", this.giftId);
    }

    public int getBuyNum() {
        return buyNum;
    }

    public void putBuyNum(int buyNum) {
        if (this.buyNum != buyNum) {
            this.buyNum = buyNum;
            updateRedisHashField(getKey(), "buyNum", this.buyNum);
        }
    }

    public void addBuyNum(int value) {
        this.buyNum += value;
        updateRedisHashField(getKey(), "buyNum", this.buyNum);
    }

    public long getResetTime() {
        return resetTime;
    }

    public void putResetTime(long resetTime) {
        if (this.resetTime != resetTime) {
            this.resetTime = resetTime;
            updateRedisHashField(getKey(), "resetTime", this.resetTime);
        }
    }

    public void addResetTime(long value) {
        this.resetTime += value;
        updateRedisHashField(getKey(), "resetTime", this.resetTime);
    }

    public String getActListKey() {
        return actListKey;
    }

    public void putActListKey(String actListKey) {
        if (!this.actListKey.equals(actListKey)) {
            this.actListKey = actListKey;
            updateRedisHashField(getKey(), "actListKey", this.actListKey);
        }
    }

}